package com.qf.Servlet;

import com.qf.entity.Book;
import com.qf.service.impl.BookServiceImpl;
import com.qf.util.MyUtil;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author Wy
 * @version V1.0
 * @Project day41-3
 * @Package ${PACKAGE_NAME}
 * @Description: ${TODO}
 * @Date 2022/5/14 16:47
 */
@WebServlet(name = "AddBookServlet",urlPatterns = "/addBook")
public class AddBookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        String bookName = request.getParameter("bookName");
        String author =  request.getParameter("author");
        Double price = Double.parseDouble(request.getParameter("price"));
        String description = request.getParameter("description");
        String publisher = request.getParameter("publisher");
        if(MyUtil.isEmpty(bookName)){
            session.setAttribute("msg","书名不能为空");
            response.sendRedirect(request.getContextPath()+"/addBook.jsp");
            return;
        }




        Book book = new Book(null, bookName, price, description, author, publisher);
        BookServiceImpl bookService = new BookServiceImpl();
        boolean result = bookService.addBook(book);
        if(result){
            response.sendRedirect(request.getContextPath()+"/bookList");

        }else{
            session.setAttribute("msg","添加失败");
            response.sendRedirect(request.getContextPath()+"/addBook.jsp");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                    doPost(request,response);
    }
}
